<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">浅宇医疗</div>
            <el-form :model="form" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-user"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="form.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-unlock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data: function() {
        return {
            form: {
                username: '',
                password: '',
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    const params = new URLSearchParams();
                    params.append('username', this.form.username);
                    params.append('password', this.form.password);
                    this.$axios.post('authority/login', params).then(response => {
                        if (response.data.code === 0) {
                            this.$message.success({
                                message: response.data.message,
                                duration: 1000
                            })
                            //配置全局token请求头
                            this.$axios.defaults.headers = {
                                'token': response.data.token
                            }
                            //提交token
                            this.$store.commit('setToken', response.data.token)
                            //提交menu给store
                            this.buildMenu(response.data.roles)
                            //保存用户名
                            sessionStorage.setItem('uid', response.data.uid)
                            sessionStorage.setItem('id', response.data.id)
                            sessionStorage.setItem('roles',response.data.roles)
                            //路由跳转主界面
                            this.$router.push({
                                path : '/dashboard'
                            })
                        } else {
                            this.$message.error({
                                message: response.data.message,
                                duration: 2000
                            })
                        }
                    }).catch(error => {
                        console.log(error)
                    })
                } else {
                    this.$message.error('请输入账号和密码');
                    return false;
                }
            });
        },
        buildMenu(roles) {
            this.$axios.get('authority/menu',{
                params:{"roles":roles}
            }).then(response => {
                // if (response.data.code === 0) {
                    this.$store.commit('setMenu', response.data)
                // } else {
                //     this.$message.error({
                //         message: response.data.message,
                //         duration: 1000
                //     })
                // }
            }).catch(error => {
                console.log(error)
            })
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: 100%;
}
.login-bg {
    width: 100%;
    height: 100%;
    background: #3E3E3E;
    position: relative;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
